package fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.admintor.humaninteraction.Data;
import com.example.admintor.humaninteraction.LoginActivity;
import com.example.admintor.humaninteraction.R;

import java.lang.reflect.Field;

/**
 * Created by Admintor on 2017/11/9.
 */

public class ElevenFragment extends Fragment implements View.OnClickListener {
    ImageView by_open, by_close, dr_close, gr_close, hum_jia, hum_jian;
    boolean state1, state2, state3, state4;
    TextView hum_str;

    ImageView bai, timer;
    Button open;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_eleven, container, false);
        FindView(view);
        Onclick();
        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);


        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 定位组件
     */
    private void FindView(View view) {
        by_open = (ImageView) view.findViewById(R.id.by_open);
        by_close = (ImageView) view.findViewById(R.id.by_close);
        hum_jia = (ImageView) view.findViewById(R.id.hum_jia);
        hum_jian = (ImageView) view.findViewById(R.id.hum_jian);
        gr_close = (ImageView) view.findViewById(R.id.gr_close);
        dr_close = (ImageView) view.findViewById(R.id.dr_close);
        hum_str = (TextView) view.findViewById(R.id.hum_str);
        bai = (ImageView) view.findViewById(R.id.bai);
        timer = (ImageView) view.findViewById(R.id.timer);
        open = (Button) view.findViewById(R.id.open);
    }

    /**
     * 组件按键响应事件
     */
    private void Onclick() {
        by_open.setOnClickListener(this);
        by_close.setOnClickListener(this);
        dr_close.setOnClickListener(this);
        gr_close.setOnClickListener(this);
        hum_jia.setOnClickListener(this);
        hum_jian.setOnClickListener(this);
        open.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.open:
                LoginActivity.landing.setKeyByUiid(Data.NUAN_FENG_JI, 1);
                break;
            /** 摆页控制 */
            case R.id.by_open:
                if (state1 == false) {
                    by_open.setBackgroundResource(R.mipmap.by_open_down);
                    bai.setBackgroundResource(R.mipmap.b_s_open);
                    state1 = true;
                } else {
                    by_open.setBackgroundResource(R.mipmap.by_open_up);
                    bai.setBackgroundResource(R.mipmap.b_s_close);
                    state1 = false;
                }
                LoginActivity.landing.setKeyByUiid(Data.NUAN_FENG_JI, 4);

                break;
            /** 挡位控制 */
            case R.id.by_close:
                if (state2 == false) {
                    state2 = true;
                    by_close.setBackgroundResource(R.mipmap.by_close_down);

                } else {
                    state2 = false;
                    by_close.setBackgroundResource(R.mipmap.by_close_up);
                }
                LoginActivity.landing.setKeyByUiid(Data.NUAN_FENG_JI, 2);

                break;
            /** 定时控制 */
            case R.id.dr_close:
                if (state3 == false) {
                    state3 = true;
                    dr_close.setBackgroundResource(R.mipmap.dr_open_down);
                    timer.setBackgroundResource(R.mipmap.r_s_open);
                } else {
                    state3 = false;
                    dr_close.setBackgroundResource(R.mipmap.dr_open_up);
                    timer.setBackgroundResource(R.mipmap.r_s_close);
                }
                LoginActivity.landing.setKeyByUiid(Data.NUAN_FENG_JI, 3);

                break;
            /** 清除控制 */
            case R.id.gr_close:
                if (state4 == false) {
                    state4 = true;
                    gr_close.setBackgroundResource(R.mipmap.gr_open_down);

                } else {
                    state4 = false;
                    gr_close.setBackgroundResource(R.mipmap.gr_open_up);
                }
                LoginActivity.landing.setKeyByUiid(Data.NUAN_FENG_JI, 5);

                break;
            /** 温度加 */
            case R.id.hum_jia:
                if (hum <= 30) {
                    hum_str.setText((hum++) + "℃");
                    LoginActivity.landing.setKeyByUiid(Data.NUAN_FENG_JI, 6);

                }

                break;
            /** 温度减 */
            case R.id.hum_jian:
                if (hum >= 16) {
                    hum_str.setText((hum--) + "℃");
                    LoginActivity.landing.setKeyByUiid(Data.NUAN_FENG_JI, 7);

                }
                break;
        }
    }

    int hum = 22;
}